-- Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
--
-- this file licensed under the Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at: http://license.coscl.org.cn/MulanPSL2
--
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
-- PURPOSE.
-- See the Mulan PSL v2 for more details.
--
-- Author: auto generate
-- Create: 2025-8-19
-- Description: DO NOT EDIT; Code generated by "${project_name}_app.lua.mako"

local class = require 'mc.class'
local service = require 'my_app.service'

-- 新增ipmi相关文件
local impi_struct = require 'my_app.ipmi.ipmi'
local ipmi_msg = require 'my_app.ipmi.ipmi_message'
local ipmi = require 'ipmi'
local object_manage = require 'mc.mdb.object_manage'

local my_app = class(service)

function my_app:ctor()

end

function my_app:init()
    self.super.init(self)
    self.my_mds_model = self:CreateMyMDSModel(1, function(object) -- 创建一个mds对象实例，名字为1
        object.ObjectName = "MyMDSModel_1"  -- 在回调函数中进行对象的属性赋值
        object.WelcomeMessage = "Hello OpenUBMC!"
        object.SecretNumber = 330
    end)
    self:register_ipmi()
    self:register_mds_callback()
end

function my_app:register_ipmi()
   self:register_ipmi_cmd(ipmi_struct.GetSecretNumber, function(req, ctx, ...)
        return ipmi_msg.GetSecretNumberRsp.new(ipmi.types.Cc.Success, self.my_mds_model.SecretNumber)
    end)
    self:register_ipmi_cmd(ipmi_struct.SetSecretNumber, function(req, ctx, ...)
        if req.Data > 630 then
            return ipmi_msg.SetSecretNumberRsp.new(ipmi.types.Cc.InvalidFieldRequest)
        end
        self.my_mds_model.SecretNumber = req.Data
        return ipmi_msg.SetSecretNumberRsp.new(ipmi.types.Cc.Success)
    end)

end

function my_app:register_mds_callback()
    object_manage.on_add_object(self.bus, function(class_name, object, position)
        if class_name == 'MyCSRModel' then
            self.my_csr_model = object
        end
    end)
    object_manage.on_add_object_complete(self.bus, function(position)
    end)
    object_manage.on_delete_object(self.bus, function(class_name, object, position)
        if self.my_csr_model == object then
            self.my_csr_model = nil
        end
    end)
    object_manage.on_delete_object_complete(self.bus, function(position)
    end)
end

return my_app
